package com.xnx3.banmahuishou.superadmin.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.xnx3.wangmarket.shop.store.vo.GoodsDetailsVO;
import com.xnx3.wangmarket.shop.store.vo.GoodsListVO;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.j2ee.service.SqlCacheService;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.Goods;
import com.xnx3.wangmarket.shop.core.entity.GoodsData;

/**
 * 商品管理
 * @author 王晓龙
 */
@Controller(value="BanmahuishouSuperadminGoodsController")
@RequestMapping("/superadmin/goods/")
public class GoodsController extends BaseController {
	@Resource
	private SqlService sqlService;
	@Resource
	private SqlCacheService sqlCacheService;

	/**
	 * 查看商品列表
	 * @author 王晓龙
	 * @param everyNumber 每页显示多少条数据。取值 1～100，最大显示100条数据，若传入超过100，则只会返回100条
	 * @param currentPage 要查看第几页，如要查看第2页，则这里传入 2
	 */
	@ResponseBody
	@RequestMapping(value = "/list${api.suffix}" ,method = {RequestMethod.POST})
	public GoodsListVO list(HttpServletRequest request,
							@RequestParam(value = "everyNumber", required = false, defaultValue = "15") int everyNumber) {
		GoodsListVO vo = new GoodsListVO();
		
		//创建Sql
		Sql sql = new Sql(request);
		//配置查询那个表
		sql.setSearchTable("shop_goods");
		//查询条件
		sql.appendWhere("isdelete = " + Goods.ISDELETE_NORMAL);
//		sql.appendWhere("storeid = " + getStoreId());
		//配置按某个字端搜索内容
		sql.setSearchColumn(new String[] {"title","typeid="});
		// 查询数据表的记录总条数
		int count = sqlService.count("shop_goods", sql.getWhere());
		
		// 配置每页显示15条
		Page page = new Page(count, everyNumber, request);
		// 查询出总页数
		sql.setSelectFromAndPage("SELECT * FROM shop_goods ", page);
		//选择排序方式 当用户没有选择排序方式时，系统默认降序排序
		sql.setDefaultOrderBy("rank ASC");
		//其余可以选择的排序字段
		sql.setOrderByField(new String[]{"id","inventory","sale","price"});
		// 按照上方条件查询出该实体总数 用集合来装
		List<Goods> list = sqlService.findBySql(sql,Goods.class);
		
		vo.setList(list);
		vo.setPage(page);
		//日志记录
		ActionLogUtil.insert(request, getUserId(), "查看商品列表");
		return vo;
	}

	/**
	 * 获取商品与商品详情
	 * @author 王晓龙
	 * @param id 商品id
	 */
	@ResponseBody
	@RequestMapping(value = "getGoods${api.suffix}" ,method = {RequestMethod.POST})
	public GoodsDetailsVO getGoods(HttpServletRequest request,
								   @RequestParam(value = "id", required = false, defaultValue = "0") int id) {
		GoodsDetailsVO vo = new GoodsDetailsVO();

		if(id < 1){
			vo.setBaseVO(GoodsDetailsVO.FAILURE, "请传入商品id");
			return vo;
		}

		//查找商品
		Goods goods = sqlService.findById(Goods.class, id);
		if(goods == null){
			vo.setBaseVO(BaseVO.FAILURE, "商品不存在");
		}
//		if(goods.getStoreid() - getStoreId() != 0){
//			vo.setBaseVO(BaseVO.FAILURE, "商品不属于你");
//		}
		vo.setGoods(goods);
		//查找商品描述
		GoodsData goodsData = sqlService.findAloneByProperty(GoodsData.class, "id", id);
		vo.setGoodsData(goodsData);

		ActionLogUtil.insert(request, getUserId(), "查看商品ID为" + (id == 0 ? "":id)+ "的详情，跳转到编辑页面");
		return vo;
	}
	
	/**
	 * 添加修改商品
	 * @author 王晓龙
	 * @param inputGoods 接收参数的实体类
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/save${api.suffix}",method = {RequestMethod.POST})
	public com.xnx3.j2ee.vo.BaseVO save(HttpServletRequest request,Goods inputGoods) {

		Integer id = inputGoods.getId();
		//创建一个实体
		Goods goods;
		if(id == null) {
			// 添加
			goods = new Goods();
			goods.setIsdelete(Goods.ISDELETE_NORMAL);
		}else {
			//修改
			goods = sqlService.findById(Goods.class, id);
			if(goods == null){
				return error("要修改的商品不存在");
			}

		}
		if(!StringUtils.hasLength(inputGoods.getTitle())) {
			return error("请输入名称");
		}
		if(!StringUtils.hasLength(inputGoods.getTitlepic())) {
			return error("请上传图片");
		}
		if(!StringUtils.hasLength(inputGoods.getUnits())) {
			return error("请输入单位");
		}
		if(inputGoods.getTypeid() == null) {
			return error("请选择分类");
		}
		if(inputGoods.getMinMoney() == null) {
			return error("请输入最小价格");
		}
		if(inputGoods.getMaxMoney() == null) {
			return error("请输入最大价格");
		}
		if(inputGoods.getOnceMinNum() == null) {
			return error("请输入上门回收最少数量");
		}
		//给实体赋值
		goods.setTitle(inputGoods.getTitle());
		goods.setTitlepic(inputGoods.getTitlepic());
		goods.setUnits(inputGoods.getUnits());
		goods.setTypeid(inputGoods.getTypeid());
		goods.setMinMoney(inputGoods.getMinMoney());
		goods.setMaxMoney(inputGoods.getMaxMoney());
		goods.setOnceMinNum(inputGoods.getOnceMinNum());
		goods.setIntro(inputGoods.getIntro());
		//保存实体
		sqlService.save(goods);
		
		//日志记录
		ActionLogUtil.insertUpdateDatabase(request, goods.getId(),"Id为" + goods.getId() + "的商品添加或修改，内容:" + goods.toString());
		return success();
	}


	/**
	 * 保存完成后，删除缓存，已达到更新缓存目的
	 * @param goodsid 要删除的缓存的商品id
	 */
	private void deleteGoodsCache(int goodsid){
		sqlCacheService.deleteCacheById(Goods.class, goodsid);
		sqlCacheService.deleteCacheById(GoodsData.class, goodsid);
	}
	
	/**
	 * 删除商品
	 * @author 王晓龙
	 * @param goodsid 删除商品id,对应goods.id
	 */
	@ResponseBody
	@RequestMapping(value="/delete${api.suffix}",method = {RequestMethod.POST})
	public BaseVO delete(HttpServletRequest request,
			@RequestParam(value = "goodsid",defaultValue = "0", required = false) int goodsid) {
		if(goodsid < 1) {
			return error("请传入id参数");
		}
		
		Goods goods = sqlService.findById(Goods.class, goodsid);
		if(goods == null) {
			return error("根据ID,没查到该实体");
		}
//		if(goods.getStoreid() - getStoreId() != 0){
//			return error("商品不属于你");
//		}
		goods.setIsdelete(Goods.ISDELETE_DELETE);
		sqlService.save(goods);
		
		//操作完成后，删除缓存，已达到更新缓存目的
		deleteGoodsCache(goods.getId());
				
		//日志记录
		ActionLogUtil.insertUpdateDatabase(request, "删除ID是" + goodsid + "的商品", "删除内容:" + goods.toString());
		return success();
	}

}
